package com.cs.system.controller;

import com.cs.core.util.ParamUtil;
import com.cs.framework.common.HttpCode;
import com.cs.framework.domain.PageVO;
import com.cs.framework.domain.R;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.cs.system.entity.SysDict;
import com.cs.system.service.SysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 字典数据表 接口定义
 * @author wind
 * @date 2025/08/08 17:52:02
 * @version V1.0
 */
@RestController
@RequestMapping("/api/sysDict")
public class SysDictController {

    private final Logger logger = LoggerFactory.getLogger(SysDictController.class);

    @Autowired
    private SysDictService sysDictService;

    /**
     * 添加记录接口
     * @param r
     * @return
     */
    @PostMapping("/")
    public R save(@RequestBody SysDict r) {
        logger.info("SysDictController.save param: r is {}", r);
        boolean flag = sysDictService.save(r);
        return HttpCode.flag(flag, r.getId());
    }


    /**
     * 修改记录接口
     * @param r
     * @return
     */
    @PutMapping("/")
    public R update(@RequestBody SysDict r) {
        logger.info("SysDictController.update param: r is {}", r);
        ParamUtil.notNull(r.getId(), "id is null");
        boolean flag = sysDictService.updateById(r);
        return HttpCode.flag(flag);
    }
	
    /**
     * 删除记录接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        logger.info("SysDictController.delete param: id is {}", id);
        boolean flag = sysDictService.removeById(id);
        return HttpCode.flag(flag);
    }

    /**
     * 单条记录查询接口
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get(@PathVariable("id") Long id) {
        logger.info("SysDictController.get param: id is {}", id);
        SysDict entity = sysDictService.getById(id);
        return HttpCode.success(entity);
    }

    /**
     * 批量查询记录接口
     * @param r
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody SysDict r) {
        logger.info("SysDictController.list param: r is {}", r);
        List<SysDict> list = sysDictService.list(QueryWrapper.create(r));
        return HttpCode.success(list);
    }

    /**
     * 分页查询记录接口
     * @param r
     * @return
     */
    @PostMapping("/page/list")
    public R pageList(@RequestBody SysDict r){
        logger.info("SysDictController.pageList param: r is {}", r);
        Page<SysDict> page = Page.of(r.getPage(), r.getSize());
        sysDictService.page(page, QueryWrapper.create(r));
        return HttpCode.success(PageVO.of(page));
    }
}
